package com.ctgu.examonline.controller;

import com.alibaba.fastjson.JSON;
import com.ctgu.examonline.entity.QueryInfo;
import com.ctgu.examonline.entity.Student;
import com.ctgu.examonline.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //登录
    @CrossOrigin //跨域
    @RequestMapping("/login")
    public String login2(@RequestBody Student student) {
        if (studentService.login(student.getStu_no(), student.getStu_pwd()) != null) {
            System.out.println(studentService.login(student.getStu_no(), student.getStu_pwd()));
            return "ok";
        } else {
            System.out.println("错误");
            return "false";
        }
    }

    //查询全部
    @RequestMapping("/selectAllStudent")
    public List<Student> selectAll() {
        return studentService.selectAllStudent();
    }

    //查询全部学生——自带分页查询
    @RequestMapping("/getAllStudentList")
    public String getAllStudentList(QueryInfo queryInfo) {
        System.out.println(queryInfo);
        int numbers = studentService.getStudentCounts("%" + queryInfo.getQuery() + "%");// 获取数据总数
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Student> students = studentService.getAllStudent("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers", numbers);
        res.put("students", students);
        System.out.println("总条数" + numbers);
        return JSON.toJSONString(res);
    }

    //添加学生 -- 应放在管理员或老师下（暂不确定）
    // 注意 学生ID主键不重复和学生专业外键必须取教师表中已有的专业。
    @RequestMapping("/add")
    public String AddOne(Student student) {
        studentService.save(new Student(null, "测试一", "19981121", Integer.valueOf("20001011"), "2019"));
        return "student";
    }
}


